package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.basics.exceptions.ThrowPageException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.BBusiness;
import com.sxbbc.common.core.entity.jz.BMenus;
import com.sxbbc.common.core.entity.jz.BRoles;
import com.sxbbc.common.core.enums.BusinessScopeEnums;
import com.sxbbc.common.core.enums.BusinessTypeEnums;
import com.sxbbc.common.core.mapper.jz.BBusinessMapper;
import com.sxbbc.common.core.mapper.jz.BMenusMapper;
import com.sxbbc.common.core.mapper.jz.BRolesMapper;
import com.sxbbc.common.core.service.IBRolesService;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商家角色表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-08
 */
@Service
public class BRolesServiceImpl extends ServiceImpl<BRolesMapper, BRoles> implements IBRolesService {

    @Autowired
    private BMenusMapper bMenusMapper;
    @Autowired
    private BBusinessMapper bBusinessMapper;

    @Override
    public List<BMenus> queryMenusByRoleId(Integer id, Integer bid) {
        String menuIds = "";
        BRoles role = null;
        if (id != null) {
            role = super.getById(id);
            if (role == null) {
                throw new ThrowPageException("未找到角色信息");
            }
            menuIds = "," + role.getMenuIds() + ",";
        }
        QueryWrapper<BMenus> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StaticUtils.STATUS_YES);

        BBusiness business = bBusinessMapper.selectById(bid);
        if (!CalcuUtils.getInstance().isEquals(business.getOnOff(), BusinessScopeEnums.OFFLINE.getId())
                || !CalcuUtils.getInstance().isEquals(business.getBType(), BusinessTypeEnums.OWN_BUSINESS.getId())) {
            wrapper.ne("id", 9);
        }
        wrapper.orderByAsc("id,type");
        try {
            List<BMenus> list = bMenusMapper.selectList(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<BMenus> list = bMenusMapper.selectList(wrapper);
        for (BMenus l : list) {
            if (StringUtils.isNotBlank(menuIds)) {
                String m = "," + l.getId() + ",";
                if (menuIds.indexOf(m) > -1) {
                    l.setCheck("checked");
                }
            }
        }
        return list;
    }
}
